package com.example.android.BluetoothChat;

import java.text.DecimalFormat;

public class LookupTable {

	public static String lookup(String hexString) {
		// Unit : kWh
		double y;
		double m = 0.0324;
		double x = hexStringToDouble(hexString);
		double c = -0.867;
		y = m * x + c;
		DecimalFormat df = new DecimalFormat("#.##");
		if (y < 0) {
			return df.format(0);
		} else if (y * 220 / 1000 > 3.3) {
			return df.format(3.4);
		}
		return df.format(y * 220 / 1000);
	}

	public static Double hexStringToDouble(String hexString) {
		try {
			return (double) Integer.parseInt(hexString, 16);
		} catch (NumberFormatException e) {
			return -1.0;
		}
	}
}
